package com.heu.blood.match.controller;

import com.heu.blood.application.entity.BloodApplicationItemEntity;
import com.heu.blood.application.vo.BloodApplicationVo;
import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.match.dto.req.BloodTransfusionStateChangeReqDTO;
import com.heu.blood.match.dto.resp.MathchedPatientInformationRespDTO;
import com.heu.blood.match.dto.resp.SearchNewMatchBloodRespDTO;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.match.entity.BloodMatchEntity;
import com.heu.blood.match.exception.TimeException;
import com.heu.blood.match.service.BloodMatchService;
import com.heu.blood.match.vo.*;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2023-06-16 00:23:13
 */
@RestController
@RequestMapping("match/bloodmatch")
public class BloodMatchController {
    @Autowired
    private BloodMatchService bloodMatchService;

    /**
     * 分页查询交叉配血表信息
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "交叉配血", operate = "分页查询交叉配血信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bloodMatchService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据id查询交叉配血信息")
    public R info(@PathVariable("id") Long id){
		BloodMatchEntity bloodMatch = bloodMatchService.getById(id);

        return R.ok().put("data", bloodMatch);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "交叉配血", operate = "保存交叉配血信息")
    public R save(@RequestBody BloodMatchEntity bloodMatch){
		bloodMatchService.save(bloodMatch);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BloodMatchEntity bloodMatch){
		bloodMatchService.updateById(bloodMatch);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "交叉配血", operate = "删除交叉配血信息")
    public R delete(@RequestBody Long[] ids){
		bloodMatchService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 条件查询交叉配血表信息
     * @param bloodMatchEntity
     * @return
     */
    @RequestMapping("/query")
    @ControllerLogAnnotation(module = "交叉配血", operate = "条件查询交叉配血信息")
    public R query(@RequestBody BloodMatchEntity bloodMatchEntity){
        List<BloodMatchEntity> bloodMatchEntities = bloodMatchService.query(bloodMatchEntity);
        return R.ok().put("list", bloodMatchEntities);
    }

    /**
     * 根据配血单号查询配血记录表
     * @param matchId
     * @return
     */
    @GetMapping("/queryByMatchId")
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据配血单号查询配血记录表")
    public R queryByMatchId(String matchId){
        BloodMatchEntity bloodMatchEntity = bloodMatchService.queryByMatchId(matchId);
        return R.ok().put("list", bloodMatchEntity);
    }

    /**
     * 查找新鲜血液
     * @param requestParam
     * @return
     */
    @RequestMapping("/searchBlood")
    @ControllerLogAnnotation(module = "交叉配血", operate = "查找新的血液")
    public R searchBlood(@RequestBody SearchNewMatchBloodRespDTO requestParam){
        List<BloodInformationEntity> bloodInformationEntities = bloodMatchService.searchBlood(requestParam);
        return R.ok().put("list", bloodInformationEntities);
    }

    /**
     * 判定交叉配血是否成功
     * @param matchId
     * @return
     */
    @PostMapping("/verify")
    @ControllerLogAnnotation(module = "交叉配血", operate = "判断交叉配血是否成功")
    public R verify(String matchId){
       Pair<Boolean, String> res = bloodMatchService.verify(matchId);
       if (res.getLeft()){
           return R.ok(res.getRight());
       } else {
           return R.error(res.getRight());
       }
    }

    /**
     * 根据患者id来查询患者配血表记录
     * @param patientId
     * @return
     */
    @GetMapping("/getByPatientId")
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据患者id来查询患者配血表记录")
    public R getByPatientId(String patientId){
        List<BloodMatchEntity> bloodMatchEntities = bloodMatchService.getByPatientId(patientId);
        return R.ok().put("list", bloodMatchEntities);
    }
    /**
     * 通过患者id来寻找所有配血成功的血液
     * @param patientId
     * @return
     */
    @GetMapping("/searchMatchedBlood")
    @ControllerLogAnnotation(module = "交叉配血", operate = "通过患者id来寻找所有配血成功的血液")
    public R searchMatchedBlood(String patientId){
        List<BloodMatchEntity> bloodMatchEntities = bloodMatchService.searchMatchedBlood(patientId);
        return R.ok().put("list", bloodMatchEntities);
    }

    /**
     * 同步更改申请单和血液状态
     */
    @RequestMapping("/changeBloodTransfusionState")
    @ControllerLogAnnotation(module = "交叉配血", operate = "同步更改申请单和血液状态")
    public R changeBloodTransfusionState(@RequestBody BloodTransfusionStateChangeReqDTO requestParam){
        boolean flag = bloodMatchService.changeBloodTransfusionState(requestParam);
        if(flag) return R.ok("状态更改成功");
        return R.ok("状态更改失败");

    }

    /**
     * 根据血液id查询所有配血的患者信息
     */
    @GetMapping("/getMatchedPationImforamtion")
    @ControllerLogAnnotation(module = "交叉配血", operate = "同步更改申请单和血液状态")
    public R getMatchedPationImforamtion(String bloodId){
        List<MathchedPatientInformationRespDTO> mathchedPatientInformationRespDTOS = bloodMatchService.searchMatchedPatientInformation(bloodId);
        return R.ok().put("data", mathchedPatientInformationRespDTOS);
    }

    /**
     * 根据输血单号获取还未发血但是已经配血成功的记录
     */
    @GetMapping("/searchSuccessMatched")
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据输血单号获取还未发血但是已经配血成功的记录")
    public R searchSuccessMatched(String transfusionId){
        List<BloodMatchEntity> bloodMatchEntities = bloodMatchService.searchSuccessMatched(transfusionId);
        return R.ok().put("data", bloodMatchEntities);
    }

    /**
     * 该功能为患者维度的历史的配血记录进行查询
     */
    @GetMapping("/getBloodMatchRecordQueryPatientDimension")
    @ControllerLogAnnotation(module = "交叉配血", operate = "患者维度的历史的配血记录进行查询")
    public R getBloodMatchRecordQueryPatientDimension(BloodMatchRecordQueryPatientDimensionVo bloodMatchRecordQueryPatientDimensionVo){
        PageUtils page = bloodMatchService.getBloodMatchRecordQueryPatientDimension(bloodMatchRecordQueryPatientDimensionVo);
        return R.ok().put("page", page);
    }

    /**
     * 根据BloodId查询配血的记录
     */
    @GetMapping("/searchBloodMathByBloodId")
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据BloodId查询配血的记录")
    public R searchBloodMathByBloodId(String  bloodId){
        List<BloodMatchEntity> bloodMatchList = bloodMatchService.searchBloodMathByBloodId(bloodId);
        return R.ok().put("data", bloodMatchList);
    }

    /**
     * 该功能为血袋的历史的配血记录进行查询
     */
    @GetMapping("/getBloodMatchRecordQueryBagDimension")
    @ControllerLogAnnotation(module = "交叉配血", operate = "血袋维度的历史的配血记录进行查询")
    public R getBloodMatchRecordQueryBagDimension(BloodMatchRecordQueryBagDimensionVo bloodMatchRecordQueryBagDimensionVo){
        PageUtils page = bloodMatchService.getBloodMatchRecordQueryBagDimension(bloodMatchRecordQueryBagDimensionVo);
        return R.ok().put("page", page);
    }

    /**
     * 该功能为写死状态的血袋的历史的配血记录进行查询
     */
    @GetMapping("/getBloodMatchRecordQueryBagDimensionByStatus")
    @ControllerLogAnnotation(module = "交叉配血", operate = "写死状态的血袋的历史的配血记录进行查询")
    public R getBloodMatchRecordQueryBagDimensionByStatus(BloodMatchRecordQueryBagDimensionVo bloodMatchRecordQueryBagDimensionVo){
        PageUtils page = bloodMatchService.getBloodMatchRecordQueryBagDimensionByStatus(bloodMatchRecordQueryBagDimensionVo);
        return R.ok().put("page", page);
    }


    /**
     * 找到所有可以为当前申请单提供配血操作的血液
     * @param bloodApplicationVo
     * @return
     */
    @ControllerLogAnnotation(module = "交叉配血", operate = "找到所有可以为当前申请单提供配血操作的血液")
    @RequestMapping("/getAllBloodCanUse")
    public R getAllBloodCanUse(@RequestBody BloodApplicationVo bloodApplicationVo){
        Map<Long,List<BloodInformationEntity>> data = bloodMatchService.getAllBloodCanUse(bloodApplicationVo);
        return R.ok().put("data", data);
    }

    /**
     * 找到所有不需要配血就可以用的血液
     * @param bloodApplicationItemEntity
     * @return
     */
    @ControllerLogAnnotation(module = "交叉配血", operate = "找到所有不需要配血就可以用的血液")
    @RequestMapping("/getAllBloodCanUseNoMatch")
    public R getAllBloodCanUseNoMatch(@RequestBody BloodApplicationItemEntity bloodApplicationItemEntity){
        List<BloodInformationEntity> res = bloodMatchService.getAllBloodCanUseNoMatch(bloodApplicationItemEntity);
        return R.ok().put("data", res);
    }

    /**
     * 进行交叉配血的插入
     * @param bloodApplicationVo
     * @return
     */
    @ControllerLogAnnotation(module = "交叉配血", operate = "为血袋初始录入交叉配血的信息")
    @RequestMapping("/insertBloodMatchInfo")
    public R insertBloodMatchInfo(@RequestBody BloodMatchInsertVO bloodApplicationVo){
        int flag = bloodMatchService.insertBloodMatchInfo(bloodApplicationVo);
        return R.ok();
    }
    /**
     * 根据申请项ID查询当前申请项配血信息
     * @param bloodMatchEntity
     * @return
     */
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据申请项ID查询当前申请项配血信息")
    @RequestMapping("/selectMatchListWithId")
    public R insertBloodMatchInfo(@RequestBody BloodMatchEntity bloodMatchEntity){
        List<BloodMatchEntity> res = bloodMatchService.selectMatchListWithId(bloodMatchEntity);
        return R.ok().put("data",res);
    }

    /**
     * 根据申请单id查询配血信息，包含血液状态
     * @param bloodMatchEntity
     * @return
     */
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据申请单id查询配血信息，包含血液状态")
    @RequestMapping("/selectMatchInfoWithBloodState")
    public R selectMatchInfoWithBloodState(@RequestBody BloodMatchWithBloodStateVo bloodMatchEntity){
        List<BloodMatchWithBloodStateVo> res = bloodMatchService.selectMatchInfoWithBloodState(bloodMatchEntity);
        return R.ok().put("data",res);
    }

    /**
     * 根据患者id和血液id查询配血信息和发血信息
     * @param bloodMatchEntity
     * @return
     */
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据患者id和血液id查询配血信息和发血信息")
    @RequestMapping("/getMatchAndSendInfo")
    public R getMatchAndSendInfo(@RequestBody BloodMatchEntity bloodMatchEntity){
        BloodMatchAndSendVo res = bloodMatchService.getMatchAndSendInfo(bloodMatchEntity);
        return R.ok().put("data",res);
    }

    /**
     * 根据申请单id判断血液检验是否全部正常
     * @param applicationId
     * @return
     */
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据申请单id判断血液检验是否全部正常")
    @RequestMapping("/checkBloodTestResult")
    public R checkBloodTestResult(String applicationId){
        int res = bloodMatchService.checkBloodTestResult(applicationId);
        return R.ok();
    }

    /**
     * 根据病人id返回患者的交叉配血记录
     * @param patientId
     * @return
     */
    @ControllerLogAnnotation(module = "交叉配血", operate = "根据病人id返回患者的交叉配血记录")
    @RequestMapping("/getMatchRecord")
    public R getMatchRecord(String patientId){
        List<BloodMatchEntity> bloodMatchEntityList = bloodMatchService.getMatchRecord(patientId);
        return R.ok().put("data",bloodMatchEntityList);
    }

}
